// pages/index/index/index.js
var app = getApp();
let openSocket = require('../../../wss.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    shows: false,
    connectimgurl: '/images/index/notcontent2x.png', // 未连接图片地址
    connecttitle: '未连接', // 未连接文字
    isconnect: false, // 是否连接标识
    issuccess: false, // 是否成功连接
  },

  goconnect: function() {
    let _this = this;
    _this.setData({
      shows: true
    })

  },
  close: function() {
    let _this = this;
    _this.setData({
      shows: false
    })
  },

  content: function() {

    let _this = this;

    wx.scanCode({

      onlyFromCamera: true,

      success(res) {

        ws.test();

        var result = _this.getImgData(res.result);
        //获取roomid 和mac 用做wss 连接 连接成功提示用户

        var getData = app.getData('roomid');

        console.log(getData);
       
        _this.alert('连接成功','success');

        _this.close();

        _this.changecontentstate();

      }

    })


  },

  getImgData: (data) => {

    if (!data) {

      return [];

    }

    var url = data.split('?');

    var param = url[1].split(',');

    for (var i = 0; i < param.length; i++) {

      app.data[param[i].split('=')[0]] = param[i].split('=')[1];

    }

    //设置缓存数据
    app.setData(app.data);

  },

  alert:(msg,status)=>{

      wx.showToast({

        title: msg,

        icon: status,

        duration: 2000

      })

  },


  changecontentstate: function() {

    let _this = this;

    _this.setData({

      connecttitle: '已连接',

      connectimgurl: '/images/index/connectsuccess.png'

    })

  },




  onLoad: function (options) {
    
  },

  click: function (e) {

    console.log(e);

    this.setData({
      shows: true,
      songid: e.currentTarget.dataset.songid
    })

  },
  // 点击遮罩层，显示的遮罩层与面板又隐藏起来
  close: function () {
    this.setData({
      Shows: false,

    })
  },

  collect: function () {

    //获取歌号用户收藏
    console.log(this.data.songid);


    // if (this.data.collect.iscollect) {
    //   wx.showToast({
    //     title: '已经被收藏',
    //     icon: 'loading',
    //     duration: 1000
    //   })
    //   this.close()
    // } else {
    //   if (true /* 请求收藏接口 */ ) {
    //     wx.showToast({
    //       title: '收藏成功',
    //       icon: 'success',
    //       duration: 1000
    //     })
    //     this.setData({
    //       collect: {
    //         iscollect: true
    //       }
    //     })
    //   } else {
    //     wx.showToast({
    //       title: '收藏失败',
    //       icon: 'success',
    //       duration: 1000
    //     })
    //     this.close()
    // }
    // }

  },

  onLoad:function()
  {
    openSocket.init('04:BC:87:01:24:D3', '7f0000010902002b4d3320190121095123','ogEMlxBcC3g5mS-s4EGD0EEMESnc');
    openSocket.connect("wss://wx.app.magimore.com/wss");
    
    // console.log(openSocket.event.wsstatus) ;
  },
  onShow: function () {
    this.getWxData();

    // this.wsRegister();
  },

  getWxData: function () {
    let that = this;
    wx.request({
      url: 'https://wx.api.magimore.com/api/songlist/getinfo', 
      data: {
        songlistid: "1"
      },
      method: "POST",
      header: {
        'content-type': 'application/json' 
      },
      success(res) {
        that.setData({ songList: res.data.songlist });
        // console.log(res);
      }
    })
  },

  clickSong: function (e) {
    openSocket.songselect(e.currentTarget.dataset.songid, 0);
  }




})